﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;


namespace FlexConsole
{
	public delegate void PrintMessageDelegate(string msg);

	public partial class ConsoleForm : Form
	{
		private ConSlave slave;
		private Thread readConsoleThread;
		private Thread readConsoleErrorThread;
		private List<string> CmdHistory;
		private int cmd_pos;

		public ConsoleForm(ConSlave slave)
		{
			InitializeComponent();
			this.slave = slave;
			CmdHistory = new List<string>();
			cmd_pos = 0;
			try
			{
				if (!slave.StartProcessThread())
				{
					ScreenText.Text = "Error starting console\r\n";
					return;
				}

				readConsoleThread = new Thread(ReadConsoleThread);
				readConsoleErrorThread = new Thread(ReadConsoleErrorThread);
				readConsoleThread.Start();
				readConsoleErrorThread.Start();
			}
			catch
			{
				ScreenText.Text = "Error starting console.";
			}
		}

		private void ReadConsoleThread()
		{
			while (true)
			{
				string msg = slave.ConsoleRead();
				PrintMessage(msg);
			}
		}

		private void ReadConsoleErrorThread()
		{
			while (true)
			{
				string msg = slave.ConsoleErrorRead();
				PrintMessage(msg);
			}
		}

		private void PrintMessage(string msg)
		{
			if (this.InvokeRequired)
			{
				this.Invoke(new PrintMessageDelegate(PrintMessage), new object[] { msg }); 
				return;
			}

			ScreenText.Text += msg;
			ScreenText.Select(ScreenText.Text.Length, 0);
			ScreenText.ScrollToCaret();
		}

		private void RunCmdBtn_Click(object sender, EventArgs e)
		{			
			string cmd = CommandText.Text;
			CommandText.Text = "";
			slave.Exec(cmd + "\r\n");
		}

		private void CommandText_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				string cmd = CommandText.Text;
				CommandText.Text = "";
				slave.Exec(cmd + "\r\n");
				CmdHistory.Add(cmd);
			}
			cmd_pos = CmdHistory.Count;
		}

		
		#region MenuItems
		private void setBackgroundColorToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ColorDialog cd = new ColorDialog();
			cd.ShowDialog();
			ScreenText.BackColor = cd.Color;
			CommandText.BackColor = cd.Color;
		}

		private void setToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ColorDialog cd = new ColorDialog();
			cd.ShowDialog();
			ScreenText.ForeColor = cd.Color;
			CommandText.ForeColor = cd.Color;
		}

		private void chooseFontToolStripMenuItem_Click(object sender, EventArgs e)
		{
			FontDialog fd = new FontDialog();
			fd.ShowDialog();
			ScreenText.Font = fd.Font;
			CommandText.Font = fd.Font;
		}

		private void closeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Close();
		}
		#endregion

		private void ConsoleForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (readConsoleThread != null)
			{
				readConsoleThread.Abort();
				readConsoleErrorThread.Abort();
				readConsoleThread = null;
				readConsoleErrorThread = null;
			}

			slave.StopProcessThread();
		}

		private void CommandText_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Up)
			{
				if (cmd_pos > 0)
				{
					e.SuppressKeyPress = true;
					CommandText.Text = CmdHistory[--cmd_pos];
					CommandText.SelectionStart = CommandText.Text.Length;
					CommandText.SelectionLength = 0;
				}
			}

			if (e.KeyCode == Keys.Down)
			{
				if (cmd_pos < CmdHistory.Count - 1)
				{
					e.SuppressKeyPress = true;
					CommandText.Text = CmdHistory[++cmd_pos];
					CommandText.SelectionStart = CommandText.Text.Length;
					CommandText.SelectionLength = 0;
				}
			}
		}
	}
}